package org.jsola.hr.web;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.NameVerifiedAddDTO;
import org.jsola.hr.dto.NameVerifiedUpdateDTO;
import org.jsola.hr.query.NameVerifiedQuery;
import org.jsola.hr.service.INameVerifiedService;
import org.jsola.hr.vo.NameVerifiedListVO;
import org.jsola.hr.vo.NameVerifiedVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 实名认证
 *
 * @author zhr
 */
@Slf4j
@RestController("hrNameVerifiedController")
@RequestMapping(HrConstants.API_PATH + "/v1/nameVerifieds")
public class NameVerifiedController implements INameVerifiedControllerAPI {

    @Autowired
    private INameVerifiedService nameVerifiedService;

    @Override
    @PostMapping
    public Result<NameVerifiedVO> save(@Valid @RequestBody NameVerifiedAddDTO nameVerifiedAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // 参数校验
        if(StringUtils.isBlank(nameVerifiedAddDTO.getIdCard())){
            throw new ParamException("身份证不能为空");
        }
        if(StringUtils.isBlank(nameVerifiedAddDTO.getEmpInfoId())){
            log.info("实名认证-员工ID：" + nameVerifiedAddDTO.getEmpInfoId());
            log.info("实名认证-请求信息" + nameVerifiedAddDTO.toString());
            throw new ParamException("员工ID不能为空");
        }
        if(StringUtils.isBlank(nameVerifiedAddDTO.getName())){
            throw new ParamException("姓名不能为空");
        }
        if(StringUtils.isBlank(nameVerifiedAddDTO.getPositiveUrl())){
            throw new ParamException("身份证正面照片不能为空");
        }
        if(StringUtils.isBlank(nameVerifiedAddDTO.getReverseUrl())){
            throw new ParamException("身份证反面照片不能为空");
        }
        return Result.success(nameVerifiedService.save(nameVerifiedAddDTO, tokenUser.getUserId(), tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody NameVerifiedUpdateDTO nameVerifiedUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改实名认证权限校验

        int count = nameVerifiedService.update(nameVerifiedUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }

        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{nameVerifiedId}")
    public Result<Integer> delete(@PathVariable Long nameVerifiedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除实名认证权限校验

        int count = nameVerifiedService.deleteByIds(tokenUser, nameVerifiedId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] nameVerifiedIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除实名认证权限校验

        int count = nameVerifiedService.deleteByIds(tokenUser, nameVerifiedIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{nameVerifiedId}")
    public Result<NameVerifiedVO> findById(@PathVariable Long nameVerifiedId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看实名认证权限校验

        return Result.success(
            nameVerifiedService.selectById(nameVerifiedId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<NameVerifiedListVO>> listAll(@Valid NameVerifiedQuery nameVerifiedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看实名认证权限校验

        return Result.success(
            nameVerifiedService.select(nameVerifiedQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<NameVerifiedListVO>> page(@Valid NameVerifiedQuery nameVerifiedQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            nameVerifiedService.selectPage(nameVerifiedQuery, tokenUser.getSiteId())
        );
    }


}

